package io.gitee.zhucan123.core.infrastructure.util;

import java.io.*;

/**
 * @author: zhuCan
 * @date: 2019/12/10 11:02
 * @description: 对象与字节流互相转换
 */
public class ByteSerialize {

  public static byte[] toByte(Object source) throws IOException {
    ByteArrayOutputStream bai = new ByteArrayOutputStream();
    ObjectOutputStream obi = new ObjectOutputStream(bai);
    obi.writeObject(source);
    return bai.toByteArray();
  }

  public static Object parseByte(byte[] bytes) throws IOException, ClassNotFoundException {
    ObjectInputStream oii = null;
    ByteArrayInputStream bis = null;
    //转换成输入字节流
    bis = new ByteArrayInputStream(bytes);
    oii = new ObjectInputStream(bis);
    return oii.readObject();
  }
}
